package com.jr.dao.impl;

import com.jr.dao.EmailDao;
import com.jr.entity.Email;
import com.jr.util.DAO;

import java.util.List;

public class EmailDaoImpl extends DAO<Email> implements EmailDao {
    @Override
    public int addEmail(Email email) throws Exception {
        String sql = "insert into email(enterprise_id,email_detail) values(?,?)";
        return update(sql,email.getEnterprise_id(),email.getEmail_detail());
    }

    /**
     * 根据企业id查询邮箱
     * @param enterpriseId 企业id
     * @return email集合
     * @throws Exception
     */
    @Override
    public List<Email> listAllEmail(Integer enterpriseId) throws Exception {
        String sql = "SELECT id,default_status,email_detail FROM email WHERE exist = 1 AND enterprise_id = ?";
        return getForList(sql,enterpriseId);
    }

    /**
     *根据id假删除邮箱
     * @param emailId
     * @return
     * @throws Exception
     */
    @Override
    public int updateEmailExist(Integer emailId) throws Exception {
        String sql = "update email set exist = 0 where id = ?";
        return update(sql,emailId);
    }

    /**
     * 根据邮箱名查询所有
     * @param emailName
     * @return
     * @throws Exception
     */
    @Override
    public Email checkEmailIsExist(String emailName) throws Exception {
        String sql = "select * from email where email_detail = ?";
        return get(sql,emailName);
    }

    /**
     * 根据id设置默认
     * @param emailId
     * @return
     * @throws Exception
     */
    @Override
    public int setEmailDefaultById(Integer emailId) throws Exception {
        String sql="update email set default_status = 0 where id = ?";
        return update(sql,emailId);
    }

    /**
     * 将所有邮箱默认改为普通
     * @return
     * @throws Exception
     */
    @Override
    public int setAllEmailDefault() throws Exception {
        String sql="update email set default_status = 1";
        return update(sql);
    }

}
